<?php

/**
 * This is the model class for table "supplier".
 *
 * The followings are the available columns in table 'supplier':
 * @property string $supplier_id
 * @property string $name
 * @property string $phone
 * @property string $address
 *
 * The followings are the available model relations:
 * @property Item[] $items
 */
class Supplier extends CActiveRecord
{

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Supplier the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'supplier';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('supplier_id', 'length', 'max' => 10),
            array('name', 'length', 'max' => 45),
            array('phone', 'length', 'max' => 100),
            array('address', 'length', 'max' => 200),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('supplier_id, name, phone, address', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'items' => array(self::HAS_MANY, 'Item', 'supplier_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'supplier_id' => ProductModule::t('Id', 'supplier'),
            'name' => ProductModule::t('Name', 'supplier'),
            'phone' => ProductModule::t('Phone', 'supplier'),
            'address' => ProductModule::t('Address', 'supplier'),
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('supplier_id', $this->supplier_id, true);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('phone', $this->phone, true);
        $criteria->compare('address', $this->address, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    public function listSupplier()
    {
        $criteria = new CDbCriteria;

        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                    'sort' => array(
                        'defaultOrder' => 'supplier_id desc'
                    ),
                    'pagination' => array(
                        'pageSize' => 5
                    ),
                ));
    }

    public function findIDByName($name)
    {
        $criteria = new CDbCriteria;
        $criteria->compare('name', trim($name));
        $supplier = $this->find($criteria);
        return (!empty($supplier)) ? $supplier->supplier_id : 1;
    }

}